package org.ielts.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	private static Pattern p2=Pattern.compile("^(\\d+)$");
	private static Pattern p3=Pattern.compile("(\\d+)次");

	public static int getTimes(Object timesObj){
		int result=0;
		if(timesObj!=null && timesObj instanceof Number){
			result=((Number)timesObj).intValue();
		}
		else if(timesObj!=null)
		{
			String times=(String) timesObj;
			if(p2.matcher(times).matches()){
				result=Double.valueOf(times).intValue();
			}
			else if(p3.matcher(times).matches()){
				Matcher m=p3.matcher(times);
				if(m.find()){
					result=Double.valueOf(m.group(1)).intValue();
				}
			}
		}
		return result;
	}
}
